# -*- coding: utf-8 -*-
def get_part_vars():
    """
    不仅仅只是元组或列表，只要对象是可迭代的，就可以执行分解操作。 包括字符串，文件对象，迭代器和生成器。
    :return:
    """
    data = ['ACME', 50, 91.1, (2012, 12, 21)]
    _, _shares, _price, _ = data
    print(_shares, _price)

    def gen():
        yield 'A'
        yield 'B'
        yield 'C'
        yield 'D'
        yield 'E'
        yield 'F'

    _, a, *b, _, _ = gen()
    print(a, b)


def drop_first_last(grades=[12, 34, 34, 23, 89, 100, 49, 98, 20]):
    first, *middle, last = grades
    avg = sum(grades) / len(grades)
    print(avg)


def test_3():
    records = [
        ('foo', 1, 2),
        ('bar', 'hello'),
        ('foo', 3, 4),
    ]

    def do_foo(x, y):
        print('foo', x, y)

    def do_bar(s):
        print('bar', s)

    for tag, *args in records:
        # print(tag, *args)
        if tag == 'foo':
            do_foo(*args)
        elif tag == 'bar':
            do_bar(*args)


def test_4():
    line = 'nobody:*:-2:-2:Unprivileged User:/var/empty:/usr/bin/false'
    uname, *fields, homedir, sh = line.split(':')
    print(uname, homedir, sh)
    print(fields)


def test_5():
    record = ('ACME', 50, 123.45, (12, 18, 2012))
    name, *_, (*_, year) = record
    print(name, year)

def sum(items):
    head,*tail = items
    return head + sum(tail) if tail else head


if __name__ == '__main__':
    # get_part_vars()
    # drop_first_last()
    # test_3()
    # test_4()
    # test_5()
    print(sum([5,2,1,2,4]))
